package Demo02;

import com.sun.org.apache.regexp.internal.RE;

import java.util.Comparator;
import java.util.PriorityQueue;


public class Test {

    public static void main(String[] args) {

    }
    public static void main02(String[] args) {
        Student student = new Student(10);
        Student student1 = new Student(11);
        PriorityQueue<Student> priorityQueue = new PriorityQueue<>(new StuAgeComparator());
        priorityQueue.offer(student);
        priorityQueue.offer(student1);
        System.out.println("===========");
    }
    public static void main01(String[] args) {
        Student student = new Student(10);
        Student student1 = new Student(11);
        System.out.println(student.equals(student1));
    }

}

class StuAgeComparator implements Comparator<Student> {
    @Override
    public int compare(Student o1, Student o2) {
        if(o1==o2) {
            return 0;
        }
        //null看作最小
        if(o1==null) {
            return -1;
        }
        if(o2==null) {
            return 1;
        }
        return o1.getAge()-o2.getAge();
    }
}

class Student implements Comparable{
    private int age;

    public int getAge() {
        return age;
    }

    private String name;
    public Student(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object obj) {
        //判断是不是同一个引用
        if(this==obj) {
            return true;
        }
        //如果obj是null对象或者不是Student的子类
        if(obj==null||!(obj instanceof Student)) {
            return false;
        }
        Student s = (Student)obj;
        return this.age==s.age
                && this.name.equals(s.name);
    }


    @Override
    public int compareTo(Object o) {
        //只对年龄进行比较,不管姓名
        //null是最小的
        if(o==null) {
            return 1;
        }
        Student s = (Student)o;
        return this.age-s.age;
    }
}
